#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int t,n;
const int N=20;
int r[N][N];
int main(void){
    scanf("%d",&t);
    while(t--){
        scanf("%d",&n);
        memset(r,0,sizeof(int)*400);
        //找规律构造数组
        r[0][n/2]=1;
        int pi=0;
        int pj=n/2;
        int num=2;
        int ti;
        int tj;
        while(num<=n*n){
            if(num%n==1){
                ti=pi+1;
                tj=pj;
            }
            else{
                ti=pi-1;
                tj=pj+1;
            }
            if(ti<0){
                ti=n-1;
            }
            if(tj>=n){
                tj=0;
            }
            r[ti][tj]=num++;
            pi=ti;
            pj=tj;
        }
        for(int i=0;i<n;i++){
            for(int j=0;j<n;j++){
                printf("%4d",r[i][j]);
            }
            printf("\n");
        }
    }
    return 0;
}